<?php
namespace app\api\controller;

use think\Controller;
use think\Db;
use app\api\model\Order;
/**
 *  配置信息
 */
class Config extends Basic
{
    private $model;
    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model=new \app\api\model\Config();
    }
    /**
     * @param $type
     * @param string $id
     * @return string
     * @throws \Exception
     */
    public function get_token()
    {
        $res=token();
    }

    /**
     * 统计 订单 成交总金额
     * Date: 2019/1/15
     * Time: 11:35
     * USER:GCQ
     */
    public function statistics()
    {
            $statistics=new Order();
          $data= $statistics->where('status',1)->sum('pay_price');
        json_success($data/100);
    }
    /*
 *  生成信息二维码
 */
    function get_video_code()
    {

        $path = 'pages/index/index?share_user_id='.UID;

        //实例化数据库
        $token = token();
        //获取二维码
        //$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token;
        $url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$token;
        //跳转页面
        $data = array(
            'path' => $path,
        );
        $code = json_encode($data);
        $imgInfo = $this->api_notice_increment($url, $code);
        $local = 'static/Code/' . date('YmdHis') . rand(1221212, 1212012102102) . '.png';
        file_put_contents($local, $imgInfo);
        sleep(3);
//        $code_url = $this->upImg($local);
//        print_t($code_url);
        return config('server_url').'/'.$local;
    }

    /**
     * 生成带参数二维码  判断是否是IOS解除禁用
     * Date: 2019/1/7
     * Time: 10:38
     * USER:GCQ
     */
    public function is_ios()
    {
        $path = 'pages/index/index?is_ios=1';
//        $path = 'is_ios=1';
        //实例化数据库
        $token = token();
        //获取二维码
        //$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token;
        $url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$token;
        //跳转页面
        $data = array(
            'path' => $path,
        );
        $code = json_encode($data);
        $imgInfo = $this->api_notice_increment($url, $code);
        $local = 'static/Code/' . date('YmdHis') . rand(1221212, 1212012102102) . '.png';
        file_put_contents($local, $imgInfo);
        sleep(3);
//        $code_url = $this->upImg($local);
//        print_t($code_url);
        return config('server_url').'/'.$local;

    }



    /**
     *  小能客服 获取token
     */
    public function get_access_token()
    {
//        秘钥    'MDAwMDAwMDAwMI6hfqS-iISclreSoY95g64'
//         $key = Input('post.key');
//        if($key === 'MDAwMDAwMDAwMI6hfqS-iISclreSoY95g64'){
            $token = Db('wx_token')->limit(1)->value('access_token');
            $data=[
                'access_token'=>$token,
                'expires_in'=>3600
            ];
            json_success($data);
//        }else{
//            $data=[
//                'status'=>-1,
//                'msg'=>'非法请求'
//            ];
//         echo   json_encode($data,JSON_UNESCAPED_UNICODE);
//        }
    }


    /**
     *  常见问题
     */
    public function question_list()
    {
        //查询数据
        $info= Db('question')->where(array('status'=>1))->order('sort')->field('id,question,answer')->select();
        if (!$info) {
            json_fail('暂无信息!');
        }
        // 打印数据
        json_success($info);
    }

    /*
      *  是否结束
      */
    public function time_day()
    {
        $time_day =Db('config')->getFieldBykey('time_day', 'value');
        $end_time = strtotime($time_day);
        if ($end_time < time()) {
            json_fail(1);
        } else {
            json_fail(2);
        }
    }

    /**
     * 显示关于我们
     */
    public function view_about_us()
    {
        $about=\app\admin\model\Config::getByKey('about_us');//关于我们
        $about_us=$about['value'];

        if (!empty($about_us)){
            json_success($about_us);
        }
        json_success([]);
    }

    public function getWxAccessToken($code='')
    {
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2983f00cf3822439&secret=1d573777fa1096b03a650401dd5e659a&code=$code&grant_type=authorization_code";
        $res = http_curl($url, 'get');
        if(isset($res['openid'])) {
            // 缓存access_token
            json_success(['openid'=>$res['openid']]);
        } else {
            json_fail('暂无信息!');
        }
    }


}

